package offer;

public class offerII_0034 {
    public boolean isAlienSorted(String[] words, String order) {
        int len=order.length();
        int[] num=new int[len];
        for(int i=0;i<len;++i){
            num[order.charAt(i)-'a']=i;
        }
        boolean flag=false;
        len= words.length;
        for(int i=0;i<len-1;++i){
            int minlen=Math.min(words[i].length(),words[i+1].length());
            for(int j=0;j<minlen;++j){
                if(num[words[i].charAt(j)-'a']>num[words[i+1].charAt(j)-'a']){
                    return false;
                }
                else if(num[words[i].charAt(j)-'a']<num[words[i+1].charAt(j)-'a']) break;
                if(j==minlen-1) flag=true;

            }
            if(flag&&words[i].length()>words[i+1].length()) return false;
            flag=false;
        }
        return true;
    }
}
